# _*_ coding: UTF-8 _*_
# 2020/5/11 17:15 
# PyCharm  
# Create by:LIUMINXUAN
# E-mail:liuminxuan1024@gmail.com
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

# Roberts 交叉梯度算子
saber = cv.imread("./images/yuantu/danghui_yuantu.bmp")
saber = cv.cvtColor(saber, cv.COLOR_BGR2RGB)
cv.imshow("saber", saber)
gray_saber = cv.cvtColor(saber, cv.COLOR_RGB2GRAY)
gray_saber = cv.resize(gray_saber, (200, 200))
cv.imshow("gray", gray_saber)


def RobertsOperator(roi):
    operator_first = np.array([[-1, 0], [0, 1]])
    operator_second = np.array([[0, -1], [1, 0]])
    return np.abs(np.sum(roi[1:, 1:] * operator_first)) + np.abs(np.sum(roi[1:, 1:] * operator_second))


def RobertsAlogrithm(image):
    image = cv.copyMakeBorder(image, 1, 1, 1, 1, cv.BORDER_DEFAULT)
    for i in range(1, image.shape[0]):
        for j in range(1, image.shape[1]):
            image[i, j] = RobertsOperator(image[i - 1:i + 2, j - 1:j + 2])
    return image[1:image.shape[0], 1:image.shape[1]]


# Prewitt算子

Robert_saber = RobertsAlogrithm(gray_saber)
cv.imshow("after", Robert_saber)
cv.waitKey(0)
cv.destroyAllWindows()
